/**
 * 
 */
package vanhoc.android.mediakool;

import java.util.ArrayList;

import vanhoc.android.mediakool.R;
import vanhoc.android.mediakool.custom.adapter.VideoListAdapter;
import vanhoc.android.mediakool.db.DatabaseHelper;
import vanhoc.android.medialcool.item.data.VideoItemData;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaMetadataRetriever;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

/**
 * @author Administrator
 *
 */
public class VideosActivity extends Activity implements OnItemClickListener {
	ListView listVideo;
	ArrayList<VideoItemData> arrListVideo;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.videos);
		
		initVideoList();
	}

	
	private void initVideoList() {
		listVideo	=	(ListView) findViewById(R.id.listVideo);
		
		//data
		this.arrListVideo	=	this.getVideoList();
		
		//adapter
		VideoListAdapter videoAdapter  = new VideoListAdapter(getApplicationContext(), R.id.listVideo, this.arrListVideo);
		listVideo.setAdapter(videoAdapter);
		
		listVideo.setOnItemClickListener(this);
	}


	private ArrayList<VideoItemData> getVideoList() {
		ArrayList<VideoItemData> listVideoData = new ArrayList<VideoItemData>();
		
		DatabaseHelper db = new DatabaseHelper(this);
		db.openToRead();
		Cursor cursor = db.getAllVideo();
		startManagingCursor(cursor);
		cursor.moveToFirst();
		for (int i = 0; i < cursor.getCount(); i++) {
			VideoItemData videoItem = new VideoItemData();
			videoItem.setID(Integer.parseInt(cursor.getString(0)));
			videoItem.setName(cursor.getString(1));
			videoItem.setArtist(cursor.getString(2));
			videoItem.setFormat(cursor.getString(3));
			videoItem.setSize(cursor.getString(4));
			videoItem.setDuration(cursor.getString(5));
			videoItem.setPath(cursor.getString(6));
			
			listVideoData.add(videoItem);
			cursor.moveToNext();
		}
		
		db.close();
		return listVideoData;
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		Intent it = new Intent(getApplicationContext(), VideoPlayerActivity.class);
		it.putExtra("VIDEOPATH", arrListVideo.get(position).getPath());
		startActivity(it);
	}
	
	@Override
	public void onBackPressed() {
		Intent it = new Intent(getApplicationContext(), DashboardActivity.class);
    	startActivity(it);
	}
	
}


